﻿using BZ.Logger;
using HalconDotNet;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Forms;

namespace NDK.Module.StationLogic
{
    public class Station_转盘七穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘七穴 Instance = new Station_转盘七穴();
        public Stopwatch SunWay07 = new Stopwatch();
        public string StationName = "Station07转盘7工位";

        //步骤字段、属性
        private StationStep _Step = StationStep.扫描轴运行至起始位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            StationID = 7;
            CurHoleId = 3;
            logBaseInfo = "转盘七穴流程";
            Step = StationStep.扫描轴运行至起始位;
            QueueSN = new ConcurrentQueue<string>();
            QueueImage = new ConcurrentQueue<MeasureImage>();

            VisionFlowName = $"侧Laser_{Common.CurProType}";
            VisionFlowName1 = $"侧CCD_{Common.CurProType}";
            VisionFlowName2 = $"斜CCD_{Common.CurProType}";
            SunWay07.Start();
        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.扫描轴运行至起始位:
                        if (MotionControl.AxesMove(Machine.pos_激光扫描起始位_转盘工位七))
                        {
                            Step = StationStep.判断是否工作;
                        }
                        break;

                    case StationStep.判断是否工作:
                        if (!Common.AllStationCanWork)
                            break;
                        if (!Common.DicStationWorkStatus[StationID])
                        {
                            Step = Common.DicHoleCanWork[CurHoleId] ? StationStep.触发激光开始扫描 : StationStep.工位流程完成;
                        }
                        break;

                    case StationStep.触发激光开始扫描:
                        if (!Common.IsEmptyModel)
                        {
                            if (Common.SSZN_侧激光.Start())
                            {

                            }
                            else
                            {
                                System.Windows.MessageBox.Show("七工站激光打开失败");
                                MachineStatusManager.CurrentMachineStatus = MachineStatus.Alarm;
                                break;
                            }
                        }
                        Step = StationStep.运行至激光扫描终点位;
                        break;

                    case StationStep.运行至激光扫描终点位:
                        if (MotionControl.AxesMove(Machine.pos_激光扫描终点位_转盘工位七))
                        {
                            Step = Common.IsEmptyModel ? StationStep.调用算法计算 : StationStep.获取激光扫描图片;
                        }
                        break;

                    case StationStep.获取激光扫描图片:
                        var image = Common.SSZN_侧激光.HeightImage(3000);
                        Common.SSZN_侧激光.Stop();

                        if (image != null)
                        {
                            Station_转盘三穴.Instance.QueueSN.TryDequeue(out var sn);
                            Station_转盘三穴.Instance.QueueImage.TryDequeue(out var images);
                            Station_转盘四穴.Instance.QueueImage.TryDequeue(out var imageCCD);
                            Station_转盘五穴.Instance.QueueImage.TryDequeue(out var image_短边);
                            Station_转盘六穴.Instance.QueueImage.TryDequeue(out var image_长边);
                            var allImages = new List<(string, HImage)>();
                            var Images1 = new List<(string, HImage)>() { ("斜激光", image) };
                            Images1.AddRange(image_短边.Images);
                            Images1.AddRange(image_长边.Images);

                            allImages.AddRange(images.Images);
                            allImages.AddRange(imageCCD.Images);
                            allImages.AddRange(Images1);
                            QueueSN.Enqueue(sn);
                            QueueImage.Enqueue(new MeasureImage()
                            {
                                Hole = CurHoleId,
                                Images = allImages.ToArray()
                            });
                            DataProcess.Instance.VisionProcess($"侧Laser_{Common.CurProType}", $"侧Laser_{Common.CurProType}Data", CurHoleId, sn, Images1.ToArray());
                            DataProcess.Instance.VisionProcess($"侧CCD_{Common.CurProType}", $"侧CCD_{Common.CurProType}Data", CurHoleId, sn, images.Images);
                            DataProcess.Instance.VisionProcess($"斜CCD_{Common.CurProType}", $"斜CCD_{Common.CurProType}Data", CurHoleId, sn, imageCCD.Images);
                            //var task = DataProcess.Instance.VisionProcessAsync(VisionFlowName, VisionOutPutName, CurHoleId, sn, Images1.ToArray());
                            //var task1 = task.ContinueWith( t => DataProcess.Instance.VisionProcess(VisionFlowName1, VisionOutPutName1, CurHoleId, sn, images.Images));
                            //await task1.ContinueWith(t => DataProcess.Instance.VisionProcess(VisionFlowName2, VisionOutPutName2, CurHoleId, sn, imageCCD.Images));
                            Step = StationStep.调用算法计算;
                        }
                        else
                        {
                            Step = StationStep.扫描轴运行至起始位;
                            Common.SSZN_侧激光.StopAcqHeightImage();
                            Common.SSZN_侧激光.Connect(Common.SP_工位7_深视侧激光IP);
                            //AlarmPause("激光出图异常", "工位七激光出图异常", "请检查", "重试");
                            if (AlarmPause("激光出图异常", $"七工站激光出图异常，请检查", "重新检测", "忽略报警，继续运行") == DialogResult.OK)
                            {
                                break;
                            }
                        }
                        break;

                    case StationStep.调用算法计算:

                        Step = StationStep.工位流程完成;
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if (CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.扫描轴运行至起始位;
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            扫描轴运行至起始位,
            判断是否工作,
            触发激光开始扫描,
            运行至激光扫描终点位,
            等待激光扫描完成,
            获取激光扫描图片,
            调用算法计算,
            工位流程完成
        }
    }

}
